package com.originskyseed.platform.user.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.Dept;
import com.originskyseed.platform.user.model.HighQualityUser;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IDeptService;
import com.originskyseed.platform.user.service.IHighQualityUserService;
import com.originskyseed.platform.user.service.IUserInfoService;
import com.originskyseed.platform.user.vo.request.HighQualityUserRequestVO;

/**
 * 优质用户表Controller类（OSI_HIGH_QUALITY_USER）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/highQualityUser")
@BaseLocaleResource(localeFileName = "i18n_user")
public class HighQualityUserController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(HighQualityUserController.class);

	@DubboReference
	private IHighQualityUserService highQualityUserService;

	@DubboReference
	private IDeptService deptService;

	@DubboReference
	private IUserInfoService userInfoService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/highQualityUserList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<HighQualityUser>> highQualityUserList(HttpServletRequest request) {
		BaseResponseBody<BasePage<HighQualityUser>> result = null;
		try {
			// 请求对象
			HighQualityUserRequestVO requestVO = this.getParameterBean(HighQualityUserRequestVO.class);
			// 用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String otherAccount=userInfo.getOtherAccount() == null ? "" : userInfo.getOtherAccount();
			if(userInfo.getSourceSys()!=0) //开放类 查询所有信息
			{
				// 根据范围查询
				List<String> otherAccountList = new ArrayList<>();
				if (userInfo.getType() == 6 || userInfo.getType() == 11) {
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				} else {
					Dept dept = this.deptService.selectByPrimaryKey(userInfo.getDeptId());
					List<String> deptList = new ArrayList<>();
					if (dept != null) {
						selectSonId(dept.getCode(), deptList);
						otherAccountList = this.userInfoService.selectOtherAccountByDeptIds(deptList, 2);
					}
					if (StringUtils.isNotBlank(otherAccount)) {
						otherAccountList.add(otherAccount);
					}
				}
				if (otherAccountList.size() == 0) {
					return new BaseResponseBody<BasePage<HighQualityUser>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), new BasePage(requestVO.getPageNo(), requestVO.getPageSize()));
				}
				requestVO.setChannelSubIdList(otherAccountList);
			}
			requestVO.setType(1);//标识优质用户
			// 分页查询
			BasePage<HighQualityUser> page = new BasePage<HighQualityUser>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.highQualityUserService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<HighQualityUser>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<HighQualityUser>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/highQualityUserSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<HighQualityUser> highQualityUserSave(HttpServletRequest request) {
		BaseResponseBody<HighQualityUser> result = null;
		try {
			// 请求对象
			HighQualityUser highQualityUser = this.getParameterBean(HighQualityUser.class);

			if (StringUtils.isNotBlank(highQualityUser.getId())) {
				this.highQualityUserService.updateByPrimaryKey(highQualityUser);
			} else {
				this.highQualityUserService.insertSelective(highQualityUser);
			}
			result = new BaseResponseBody<HighQualityUser>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), highQualityUser);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<HighQualityUser>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/highQualityUserDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> highQualityUserDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.highQualityUserService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}


	/**
	 * 查询组织下的部门
	 *
	 * @param code
	 * @param list
	 * @throws Exception
	 */
	private void selectSonId(String code, List<String> list) throws Exception {
		List<Dept> deptList = this.deptService.selectByParentCode(code);
		if (deptList != null) {
			for (Dept dept1 : deptList) {
				list.add(dept1.getId());
				selectSonId(dept1.getCode(), list);
			}
		}
	}
}